import unittest
from pynput.keyboard import Key, Controller
from pynput import keyboard


class TestControlKeyBoard(unittest.TestCase):

    # 模拟按键
    def test_upper(self):
        keyboard1 = Controller()
        keyboard1.press(Key.shift)
        keyboard1.release(Key.shift)

    # 键盘按下和松开监听
    def test_listen(self):
        with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
            listener.join()


# 事件方法-键盘按下监听
def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(key.char))
    except AttributeError:
        print('special key {0} pressed'.format(key))


# 事件方法-键盘按下松开监听
def on_release(key):
    print('{0} released'.format(key))
    if key == Key.esc:
        return False


if __name__ == '__main__':
    unittest.main()
